<?php
/*
 * Author: Quan Van Sinh
 * Email: sinhvnb@vietnambiz.com
 */
mb_internal_encoding('UTF-8');
$link = mysql_connect($CONFIG['db_host'], $CONFIG['db_user'], $CONFIG['db_pass']);
$connect=mysql_select_db($CONFIG['db_name'],$link);
mysql_query("set names utf8");
if($connect==null) die('Can not to connect database');

if($_GET['keyword']) {
    ExtractKeywords::AddKeyword($_GET['keyword']);
    die;
}

$freq = explode(',', $_POST['frequency']);
$freq1 = $freq[0];
$freq2 = $freq[1];
$freq3 = $freq[2];
$freq4 = $freq[3];
$freq5 = $freq[4];
$freq6 = $freq[5];

if($freq1==(-1) && $freq1==(-1) && $freq1==(-1) && $freq1==(-1) && $freq1==(-1) && $freq1==(-1)) {
    $freq[] = 8;
    $freq[] = 4;
    $freq[] = 4;
    $freq[] = 4;
    $freq[] = 4;
    $freq[] = 4;
}

ExtractKeywords::Init($freq);


ExtractKeywords::AddContent($_POST['title'],4);
ExtractKeywords::AddContent($_POST['description'],3);
ExtractKeywords::AddContent(str_ireplace('&nbsp;',' ',$_POST['content']));

$keywords=ExtractKeywords::Extract();

foreach($keywords as $keyword=>$score) {
    if ($score>3) {
        $keywordSign[] = $keyword;
        $keywordsNoSign[] = nosign($keyword);
    }
}


for($i=0;$i<count($keywordSign);$i++) {

    if(str_word_count($keywordsNoSign[$i])==1 && strlen($keywordsNoSign[$i])>=intval($freq1)) {
        $html.='<div id="k'.$i.'"><input id="key'.$i.'" type="text" size="25" value="'.utf8::UCWords($keywordSign[$i]).'" name="keyword[]" class="textbox"/>';
        $html.='<span class="keyword_remove" onclick="keyword_remove('.$i.');">Re</span> ';
        $html.='<span class="keyword_spam" onclick="keyword_spam('.$i.');">Sp</span></div>';
    }else {
        if(str_word_count($keywordsNoSign[$i])==2 && strlen($keywordsNoSign[$i])>=intval($freq2)) {
            $html.='<div id="k'.$i.'"><input id="key'.$i.'" type="text" size="25" value="'.utf8::UCWords($keywordSign[$i]).'" name="keyword[]" class="textbox"/>';
            $html.='<span class="keyword_remove" onclick="keyword_remove('.$i.');">Re</span> ';
            $html.='<span class="keyword_spam" onclick="keyword_spam('.$i.');">Sp</span></div>';
        }else {
            if(str_word_count($keywordsNoSign[$i])==3 && strlen($keywordsNoSign[$i])>=intval($freq3)) {
                $html.='<div id="k'.$i.'"><input id="key'.$i.'" type="text" size="25" value="'.utf8::UCWords($keywordSign[$i]).'" name="keyword[]" class="textbox"/>';
                $html.='<span class="keyword_remove" onclick="keyword_remove('.$i.');">Re</span> ';
                $html.='<span class="keyword_spam" onclick="keyword_spam('.$i.');">Sp</span></div>';
            }else {
                if(str_word_count($keywordsNoSign[$i])==4 && strlen($keywordsNoSign[$i])>=intval($freq4)) {
                    $html.='<div id="k'.$i.'"><input id="key'.$i.'" type="text" size="25" value="'.utf8::UCWords($keywordSign[$i]).'" name="keyword[]" class="textbox"/>';
                    $html.='<span class="keyword_remove" onclick="keyword_remove('.$i.');">Re</span> ';
                    $html.='<span class="keyword_spam" onclick="keyword_spam('.$i.');">Sp</span></div>';
                }else {
                    if(str_word_count($keywordsNoSign[$i])==5 && strlen($keywordsNoSign[$i])>=intval($freq5)) {
                        $html.='<div id="k'.$i.'"><input id="key'.$i.'" type="text" size="25" value="'.utf8::UCWords($keywordSign[$i]).'" name="keyword[]" class="textbox"/>';
                        $html.='<span class="keyword_remove" onclick="keyword_remove('.$i.');">Re</span> ';
                        $html.='<span class="keyword_spam" onclick="keyword_spam('.$i.');">Sp</span></div>';
                    }else {
                        if(str_word_count($keywordsNoSign[$i])==6 && strlen($keywordsNoSign[$i])>=intval($freq6)) {
                            $html.='<div id="k'.$i.'"><input id="key'.$i.'" type="text" size="25" value="'.utf8::UCWords($keywordSign[$i]).'" name="keyword[]" class="textbox"/>';
                            $html.='<span class="keyword_remove" onclick="keyword_remove('.$i.');">Re</span> ';
                            $html.='<span class="keyword_spam" onclick="keyword_spam('.$i.');">Sp</span></div>';
                        }
                    }
                }
            }
        }
    }
}

//foreach($keywords as $keyword=>$score) {
//    if ($score>3) {
//
//        $html.='<div id="k'.$i.'"><input id="key'.$i.'" type="text" size="25" value="'.utf8::UCWords($keyword).'" name="keyword[]" class="textbox"/>';
//        $html.='<span class="keyword_remove" onclick="keyword_remove('.$i.');">Re</span> ';
//        $html.='<span class="keyword_spam" onclick="keyword_spam('.$i.');">Sp</span></div>';
//        $i++;
//    }
//}

$html.='<div id="k'.$i.'"><input id="key'.$i.'" type="text" size="25" value="" name="keyword[]" class="textbox"/>';
$html.='<span class="keyword_remove" onclick="keyword_remove('.$i.');">Re</span> ';
$html.='<span class="keyword_spam" onclick="keyword_spam('.$i.');">Sp</span></div>';
$i=$i+1;
$html.='<div id="k'.$i.'"><input id="key'.$i.'" type="text" size="25" value="" name="keyword[]" class="textbox"/>';
$html.='<span class="keyword_remove" onclick="keyword_remove('.$i.');">Re</span> ';
$html.='<span class="keyword_spam" onclick="keyword_spam('.$i.');">Sp</span></div>';
$i=$i+1;
$html.='<div id="k'.$i.'"><input id="key'.$i.'" type="text" size="25" value="" name="keyword[]" class="textbox"/>';
$html.='<span class="keyword_remove" onclick="keyword_remove('.$i.');">Re</span> ';
$html.='<span class="keyword_spam" onclick="keyword_spam('.$i.');">Sp</span></div>';
$i=$i+1;
$html.='<div id="k'.$i.'"><input id="key'.$i.'" type="text" size="25" value="" name="keyword[]" class="textbox"/>';
$html.='<span class="keyword_remove" onclick="keyword_remove('.$i.');">Re</span> ';
$html.='<span class="keyword_spam" onclick="keyword_spam('.$i.');">Sp</span></div>';
echo $html;
die;
?>